import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

/// 描述: 尺寸扩展
/// 
/// @Author tangzhentao
///  @CreatedTime 19:59 2022/6/17
/// 

extension SizeExtension on num {
  ///[ScreenUtil.setWidth]
  double get xw => ScreenUtil().setWidth(this);

  ///[ScreenUtil.setHeight]
  double get xh => ScreenUtil().setHeight(this);

  ///[ScreenUtil.radius]
  double get xr => ScreenUtil().radius(this);

  ///[ScreenUtil.setSp]
  double get xsp => ScreenUtil().setSp(this);

  ///smart size :  it check your value - if it is bigger than your value it will set your value
  ///for example, you have set 16.sm() , if for your screen 16.sp() is bigger than 16 , then it will set 16 not 16.sp()
  ///I think that it is good for save size balance on big sizes of screen
  double get xsm => min(toDouble(), sp);

  ///屏幕宽度的倍数
  ///Multiple of screen width
  double get xsw => ScreenUtil().screenWidth * this;

  ///屏幕高度的倍数
  ///Multiple of screen height
  double get xsh => ScreenUtil().screenHeight * this;

  ///[ScreenUtil.setHeight]
  Widget get xVerticalSpace => ScreenUtil().setVerticalSpacing(this);

  ///[ScreenUtil.setVerticalSpacingFromWidth]
  Widget get xVerticalSpaceFromWidth => ScreenUtil().setVerticalSpacingFromWidth(this);

  ///[ScreenUtil.setWidth]
  Widget get xHorizontalSpace => ScreenUtil().setHorizontalSpacing(this);

  ///[ScreenUtil.radius]
  Widget get xHorizontalSpaceRadius => ScreenUtil().setHorizontalSpacingRadius(this);

  ///[ScreenUtil.radius]
  Widget get xVerticalSpacingRadius => ScreenUtil().setVerticalSpacingRadius(this);

  /// 存储空间
  String get storage {
    if (this >= 1024 * 1024 * 1024) {
      return '${(this / (1024 * 1024 * 1024)).toStringAsFixed(2)}G';
    } else if (this >= 1024 * 1024) {
      return '${(this / (1024 * 1024)).toStringAsFixed(2)}M';
    } else if (this >= 1024) {
      return '${(this / (1024 * 1024)).toStringAsFixed(2)}K';
    } else {
      if (this > 0) {
        return '${this}B';
      } else {
        return '0B';
      }
    }

  }
}

extension EdgeInsetsExtension on EdgeInsets {
  /// Creates adapt insets using r [SizeExtension].
  EdgeInsets get xei => copyWith(
    top: top.xr,
    bottom: bottom.xr,
    right: right.xr,
    left: left.xr,
  );
}

extension BorderRaduisExtension on BorderRadius {
  /// Creates adapt BorderRadius using r [SizeExtension].
  BorderRadius get xbr => copyWith(
    bottomLeft: bottomLeft.xrs,
    bottomRight: bottomRight.xrs,
    topLeft: topLeft.xrs,
    topRight: topRight.xrs,
  );
}

extension RaduisExtension on Radius {
  /// Creates adapt Radius using r [SizeExtension].
  Radius get xrs => Radius.elliptical(x.xr, y.xr);
}
